package com.dms.web.controller.system;

import java.util.Date;
import java.util.List;

import com.dms.common.core.domain.entity.SysUser;
import com.dms.system.service.ISysPlantService;
import com.dms.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysAttendance;
import com.dms.system.service.ISysAttendanceService;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;

/**
 * 考勤Controller
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Controller
@RequestMapping("/system/attendance")
public class SysAttendanceController extends BaseController
{
    private String prefix = "system/attendance";

    @Autowired
    private ISysAttendanceService sysAttendanceService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysPlantService sysPlantService;

    /**
     * 查询员工列表，默认为设备部门
     * */
    @RequiresPermissions("system:attendance:view")
    @GetMapping()
    public String attendance(@RequestParam(defaultValue = "200") Long deptId, Model m)
    {
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(deptId);
        m.addAttribute("userList",sysUserService.selectUserList(sysUser));
        m.addAttribute("plantList",sysPlantService.selectSysPlantList(null));
        return prefix + "/attendance";
    }

    /**
     * 查询考勤列表
     */
    @RequiresPermissions("system:attendance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAttendance sysAttendance)
    {
        startPage();
        List<SysAttendance> list = sysAttendanceService.selectSysAttendanceList(sysAttendance);
        return getDataTable(list);
    }

    /**
     * 导出考勤列表
     */
    @RequiresPermissions("system:attendance:export")
    @Log(title = "考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAttendance sysAttendance)
    {
        List<SysAttendance> list = sysAttendanceService.selectSysAttendanceList(sysAttendance);
        ExcelUtil<SysAttendance> util = new ExcelUtil<SysAttendance>(SysAttendance.class);
        return util.exportExcel(list, "attendance");
    }

    /**
     * 新增考勤
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考勤
     */
    @RequiresPermissions("system:attendance:add")
    @Log(title = "考勤", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAttendance sysAttendance)
    {
        return toAjax(sysAttendanceService.insertSysAttendance(sysAttendance));
    }

    /**
     * 修改考勤
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysAttendance sysAttendance = sysAttendanceService.selectSysAttendanceById(id);
        mmap.put("sysAttendance", sysAttendance);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤
     */
    @RequiresPermissions("system:attendance:edit")
    @Log(title = "考勤", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAttendance sysAttendance)
    {
        return toAjax(sysAttendanceService.updateSysAttendance(sysAttendance));
    }

    /**
     * 删除考勤
     */
    @RequiresPermissions("system:attendance:remove")
    @Log(title = "考勤", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAttendanceService.deleteSysAttendanceByIds(ids));
    }

    @ResponseBody
    @GetMapping("/rfid-record")
    public String rfid_record(SysAttendance sysAttendance){
        sysAttendance.setUserId(100L);
        sysAttendance.setPlantId(1L);
        sysAttendance.setAttendanceTime(new Date());
        sysAttendanceService.insertSysAttendance(sysAttendance);
        return "ok";
    }
}
